/*
 * @lc app=leetcode.cn id=264 lang=java
 *
 * [264] 丑数 II
 */

// @lc code=start
class Solution {
    public int nthUglyNumber(int n) {
        int[] dp = new int[n+1];

        dp[1] = 1;
        int p2 = 1, p3 = 1, p5 = 1;
        for(int i = 2; i <= n; i++) {
            int num = Math.min(Math.min(dp[p2] * 2, dp[p3] * 3), dp[p5] * 5);
            dp[i] = num;

            if(dp[p2] * 2 == num) {
                p2++;
            }

            if(dp[p3] * 3 == num) {
                p3++;
            }

            if(dp[p5] * 5 == num) {
                p5++;
            }
        }

        return dp[n];
    }
}
// @lc code=end

